package com.fengmi.controller;

import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.service.GoodsBrandService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 123
 * @Date 2022/1/24 21:13
 * @Version 1.0
 */

@RestController
@RequestMapping("brand")
@CrossOrigin
@Api(tags = "商品品牌管理")
public class GoodsBrandController {

    @Autowired
    private GoodsBrandService goodsBrandService;

    @PostMapping("page")
    @ApiOperation("分页条件查询")
    @PreAuthorize("hasRole('ROLE_XX')")
    public PageResultVO<MallGoodsBrand> page(@RequestBody @ApiParam("分页条件实体类") PageDTO<MallGoodsBrand> pageDTO) {



        return goodsBrandService.findPage(pageDTO);
    }

    @RequestMapping("save")
    @ApiOperation("添加品牌")
    public ResultVO save(@RequestBody @ApiParam("品牌实体类") MallGoodsBrand mallGoodsBrand) {
        return goodsBrandService.saveInsert(mallGoodsBrand);
    }

    @GetMapping("findOne/{brandId}")
    @ApiOperation("查询单个品牌")
    public ResultVO findOne(@PathVariable String brandId) {
        return goodsBrandService.findOneBrandById(brandId);
    }

    @GetMapping("toggleStatus/{id}/{showStatus}")
    @ApiOperation("修改品牌展示状态")
    public ResultVO toggleStatus(@PathVariable String id, @PathVariable Integer showStatus) {
        return goodsBrandService.toggleBrandShowStatus(id, showStatus);
    }

    @GetMapping("findAll")
    @ApiOperation("查询所有品牌")
    @PreAuthorize("hasRole('ROLE_TEACHER')")
    public ResultVO findAll() {
        return goodsBrandService.selectAllBrand();
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除品牌")
    public ResultVO delBrand(@PathVariable @ApiParam("品牌id") String id) {
        return goodsBrandService.deleteBrandById(id);
    }

    @PutMapping
    @ApiOperation("修改品牌")
    public ResultVO updateBrand(@RequestBody @ApiParam("品牌实体类") MallGoodsBrand mallGoodsBrand) {
        return goodsBrandService.updateBrandInfo(mallGoodsBrand);
    }

    @DeleteMapping("delMore")
    @ApiOperation("修改品牌")
    public ResultVO delMore(@RequestBody Integer[] ids) {
        //for (Integer id : ids) {
        //    System.out.println(id);
        //}
        return goodsBrandService.delMoreThanOneBrand(ids);
    }

}